package com.tarena.lbs.basic.web.service;

import com.alibaba.nacos.api.utils.StringUtils;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.basic.web.repository.AdminRepository;
import com.tarena.lbs.basic.web.repository.UserRepository;
import com.tarena.lbs.common.passport.encoder.JwtEncoder;
import com.tarena.lbs.common.passport.enums.Roles;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.po.UserPO;
import com.tarena.lbs.pojo.passport.param.AdminLoginParam;
import com.tarena.lbs.pojo.passport.param.UserLoginParam;
import com.tarena.lbs.pojo.passport.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;
    //仓储层和持久层 一个表格 一套 lbs_admin AdminRepository AdminMapper
    //lbs_user UserRepository UserMapper
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private UserRepository userRepository;
    public LoginVO doAdminLogin(AdminLoginParam param) throws BusinessException {
        //1.手机号查admin
        String phone=param.getPhone();
        AdminPO po=adminRepository.getAdminByPhone(phone);
        //2. 判断存在 断言 判断一个逻辑的语言片段 根据判断结果 一般都是抛异常
        Asserts.isTrue(po==null||po.getId()==null,new BusinessException("-2","用户不存在"));
        //3.取出来密码做判断
        Asserts.isTrue(!StringUtils.equals(param.getPassword(),po.getAccountPassword()),new BusinessException("-2","密码不正确"));
        //4.登录成功了(一些安全框架还有登录逻辑判断的 用户是否锁定 超时 是否密码过期) 生成jwt
        UserPrinciple userPrinciple=new UserPrinciple();
        //id 昵称 角色
        userPrinciple.setId(po.getId());
        userPrinciple.setNickname(po.getNickname());
        userPrinciple.setRole(Roles.getfromType(po.getAccountType()));
        //5. 创建jwt
        String jwt = jwtEncoder.generateToken(userPrinciple);
        //6. 返回前端 封装vo
        LoginVO vo=new LoginVO();
        vo.setJwt(jwt);
        //前端没用
        vo.setId(po.getId());
        vo.setNickname(po.getNickname());
        return vo;
    }

    public LoginVO doUserLogin(UserLoginParam param) throws BusinessException {
        //1 参数取手机号 查询存在 详情对象
        String phone=param.getPhone();
        UserPO userPO=userRepository.getUserByPhone(phone);
        Asserts.isTrue(userPO==null||userPO.getId()==null,
                new BusinessException("-2","电话号码不存在"));
        //2.对比提交密码和查询的po密码
        Asserts.isTrue(!StringUtils.equals(param.getPassword(),userPO.getPassword()),new BusinessException("-2","密码不正确"));
        //3.根据userPO生成userPrinciple 转化成jwt
        LoginVO vo=new LoginVO();
        vo.setId(userPO.getId());
        vo.setNickname(userPO.getNickName());
        UserPrinciple userPrinciple=new UserPrinciple();
        //id 昵称 角色
        userPrinciple.setId(userPO.getId());
        userPrinciple.setNickname(userPO.getNickName());
        userPrinciple.setRole(Roles.USER);//固定USER
        //5. 创建jwt
        String jwt = jwtEncoder.generateToken(userPrinciple);
        vo.setJwt(jwt);
        return vo;
    }
}
